package chapter2.No2_2_synchronized同步语句块.g将任意对象作为对象监视器.demo3;

/**
 * Created by Administrator on 2016/11/12.
 */
public class MyThread extends Thread {
    ServiceClass sc;

    public MyThread(ServiceClass sc) {
        this.sc = sc;
    }

    @Override
    public void run() {
        try {
//            synchronized (sc) {
                System.out.println(Thread.currentThread().getName()+" 进入run");
                if (sc.getSize() < 1) {
                    Thread.sleep(500);
                    System.out.println(Thread.currentThread().getName()+" 进入判断");
                    sc.addObject("a");
                    System.out.println(Thread.currentThread().getName()+" 进入判断");
                }
                System.out.println(Thread.currentThread().getName()+" 离开run");
//            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }
}
